package com.yyg.boot.web;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/28
 * @Description Description
 */
@Slf4j
@RestController
public class SessionController {

    @RequestMapping("/session")
    public Object springSession(@RequestParam("username") String username, HttpServletRequest request, HttpSession session) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                log.warn(cookie.getName() + "=" + cookie.getValue());
            }
        }

        Object value = session.getAttribute("username");
        if (value == null) {
            log.warn("用户不存在");
            //保存session
            session.setAttribute("username", "{username: '" + username + "', age: 30}");
        } else {
            log.warn("用户存在");
        }

        return "username=" + value;
    }

}
